package com.wjf.wjfaiagent.demo.invoke;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/*
 * 使用http方式调用ai
 * */
public class HttpApiInvoke {
    private static final String API_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";

    /*    public static void main(String[] args) {
            // API 地址
            String url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";

            // 构造请求体中的 messages 部分
            JSONObject systemMessage = JSONUtil.createObj()
                    .set("role", "system")
                    .set("content", "You are a helpful assistant.");

            JSONObject userMessage = JSONUtil.createObj()
                    .set("role", "user")
                    .set("content", "你是谁？");

            // 构造完整的 JSON 请求体
            JSONObject requestBody = JSONUtil.createObj()
                    .set("model", "qwen-plus")
                    .set("messages", JSONUtil.createArray().set(systemMessage).set(userMessage));

            // 发起 POST 请求
            HttpResponse response = HttpRequest.post(url)
                    .header("Authorization", "Bearer " + TestApiKey.API_KEY)
                    .header("Content-Type", "application/json")
                    .body(requestBody.toString())
                    .execute();

            // 输出响应结果
            System.out.println(response.body());
        }*/
    public static void main(String[] args) {
        // 构建请求体
        JSONObject requestBody = JSONUtil.createObj()
                .set("model", "qwen-vl-plus")
                .set("messages", JSONUtil.createArray().set(
                        JSONUtil.createObj().set("role", "user").set("content",
                                JSONUtil.createArray()
                                        .set(JSONUtil.createObj()
                                                .set("type", "image_url")
                                                .set("image_url", JSONUtil.createObj().set("url", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg")))
                                        .set(JSONUtil.createObj()
                                                .set("type", "text")
                                                .set("text", "这是什么？"))
                        )
                ));

        // 发送POST请求
        HttpResponse response = HttpRequest.post(API_URL)
                .header("Authorization", "Bearer " + TestApiKey.API_KEY)
                .header("Content-Type", "application/json")
                .body(requestBody.toString())
                .execute();
        System.out.println(response.body());
    }
}
